using System.Collections.Generic;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.AI
{

    public class WaitPointContainer
    {
        public int index;
        public Vector3 position;
        public Vector3 forward;

        // 归属路径点
        public int pathId;
        public int pointIndex;
        
        public bool empty = true;   // 占用状态
        public int param0;
        public int param1;
        public int param2;


        public void NpcOccupy(int npcId)
        {
            empty = false;
            param0 = npcId;
        }

        public void UnOccupy()
        {
            empty = true;
            param0 = 0;
        }
    }
    
    public class TrafficNpcTriggerContainer
    {
        public TrafficNpcTrigger npcTrigger;
        
        public List<WaitPointContainer> waitPoints;

        public TrafficLightState LightState
        {
            get
            {
                if (npcTrigger)
                {
                    return npcTrigger.lightState;
                }
                return TrafficLightState.Red;
            }
        }
        
        
        // 是否有闲置的Npc等待点位
        public bool HasEmptyWaitPoint()
        {
            if (waitPoints!=null)
            {
                for (int i = 0; i < waitPoints.Count; i++)
                {
                    if (waitPoints[i].empty)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        
        
        // 获取距离最近的等待点位
        public float GetNearestWaitPoint(Vector3 pos,out WaitPointContainer waitPointContainer)
        {
            waitPointContainer = null;
            if (!HasEmptyWaitPoint())
            {
                return -1f;
            }
            
            float min = float.PositiveInfinity;
            foreach (var waitPoint in waitPoints)
            {
                if (waitPoint.empty)
                {
                    float dis = Vector3.Distance(waitPoint.position, pos);
                    if (dis < min)
                    {
                        waitPointContainer = waitPoint;
                        min = dis;
                    }
                }
            }
            
            return min;
        }

        
        // 获得夹角最小的等待点位
        public float GetMinAngleWaitPoint(Vector3 pos,Vector3 forward,out WaitPointContainer waitPointContainer)
        {
            waitPointContainer = null;
            if (!HasEmptyWaitPoint())
            {
                return -1f;
            }
            
            float min = float.PositiveInfinity;
            foreach (var waitPoint in waitPoints)
            {
                if (waitPoint.empty)
                {
                    float angle = Vector3.Angle((waitPoint.position - pos).normalized, forward);
                    if (angle < min)
                    {
                        waitPointContainer = waitPoint;
                        min = angle;
                    }
                }
            }
            
            return min;
        }
        
        
        
    }
}